//
//  StatusListViewModel.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/21.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SVProgressHUD

class StatusListViewModel: NSObject {
    
    class func loadHomePageData(since_id:Int64,max_id:Int64,finished:(statuses:[Status]?) -> ()) {
        
        // get 请求
        let urlString = "2/statuses/home_timeline.json"
        
        guard let token = UserAccountViewModel().token else{
            SVProgressHUD.showInfoWithStatus("请重新登录")
            
            
            
            return
        }
        
        
        
        var parmeters = ["access_token":token]
        
        if since_id > 0{
            parmeters["since_id"] = "\(since_id)"
            
        }
        
        if max_id > 0 {
            parmeters["max_id"] = "\(max_id - 1 )"
            
        }
        
        
        
        NetworkTools.sharedTools.requestJSONDict(.GET, urlString: urlString, parameters: parmeters) { (dict, error) -> () in
            
            
            
            
            if error != nil {
                //执行失败的回调的
                SVProgressHUD.showInfoWithStatus("您的网络正在睡觉,请稍后...")
                finished(statuses: nil)
                return
            }
            
            //请求成功
            if let array = dict!["statuses"] as? [[String : AnyObject]] {
                //                    print(array)
                //TODO: 遍历数组中 所有的字典 做字典转模型的操作
                var list = [Status]()
                for item in array {
                    let s = Status(dict: item)
                    list.append(s)
                }
                
                //执行成功的回调
                finished(statuses: list)
            }

            
            
            
            
        }
        
    }
    
    
    
    
    
    

}
